﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Design.Formatters;
using EntLibContrib.Logging.Properties;

namespace EntLibContrib.Logging.Configuration.Design.Formatters
{
	/// <summary>
	/// Represents a <see cref="TemplateTextFormatterData"/> configuration object.
	/// </summary>
	public sealed class TemplateTextFormatterNode : FormatterNode
	{
		private string template;

		/// <summary>
		/// Initialize a new instance of the <see cref="TemplateTextFormatterNode"/> class.
		/// </summary>
		public TemplateTextFormatterNode()
			: this(new TemplateTextFormatterData(InternalResources.TemplateTextFormatterNode, InternalResources.DefaultTextFormat))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="TemplateTextFormatterNode"/> class with a <see cref="TemplateTextFormatterData"/> instance.
		/// </summary>
		/// <param name="templateTextFormatterData">A <see cref="TemplateTextFormatterData"/> instance.</param>
		public TemplateTextFormatterNode(TemplateTextFormatterData templateTextFormatterData)
		{
			if (null == templateTextFormatterData) throw new ArgumentNullException("templateTextFormatterData");

			this.template = templateTextFormatterData.Template;
			Rename(templateTextFormatterData.Name);
		}

		/// <summary>
		/// Gets the <see cref="Template"/> for the formatter.
		/// </summary>
		/// <value>
		/// The <see cref="Template"/> for the formatter.
		/// </value>
		[Required]
		[Editor(typeof(TemplateEditor), typeof(UITypeEditor))]
		[LoggingCategory("CategoryGeneral")]
		public Template Template
		{
			get { return new Template(template); }
			set { template = value.Text; }
		}

		/// <summary>
		/// Gets the <see cref="TemplateTextFormatterData"/> this node represents.
		/// </summary>
		/// <value>
		/// The <see cref="TemplateTextFormatterData"/> this node represents.
		/// </value>
		public override FormatterData FormatterData
		{
			get { return new TemplateTextFormatterData(Name, template); }
		}
	}
}
